package com.qxp.simplerpc.monitor.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.qxp.simplerpc.entity.AjaxJson;
import com.qxp.simplerpc.entity.InvokerMessage;
import com.qxp.simplerpc.entity.MonitorMessage;
import com.qxp.simplerpc.entity.PageJson;
import com.qxp.simplerpc.monitor.service.MonitorService;
import com.qxp.simplerpc.utils.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequestMapping("/")
@Controller
public class MonitorController {

    @Autowired
    private MonitorService monitorService;

    @RequestMapping("/accept")
    @ResponseBody
    public AjaxJson  accept(@RequestBody(required = false) MonitorMessage monitorMessage){
        monitorService.accept(monitorMessage);
        return  new AjaxJson().setSuccess(true);
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST )
    @ResponseBody
    public PageJson listEqual(@RequestParam(required = false) MonitorMessage monitorMessage){
        List<MonitorMessage> data = null;
        if(monitorMessage != null) {
            data = monitorService.getCachedInvokeMsg().stream()
                    .filter(m -> ClassUtils.ifAnyFieldsEqual(InvokerMessage.class, monitorMessage, m)).collect(Collectors.toList());
        }else{
            data = monitorService.getCachedInvokeMsg();
        }
        return new PageJson().setData(JSONArray.parseArray(JSON.toJSONString(data))).setStatus("success").setTotals(data.size());
    }


}
